;;; gle.el --- GLE mode for GNU Emacs
;; Copyright (C) 1995-2001 Kai Nordlund, kai.nordlund@helsinki.fi
;;
;; Based on the Fortran90 Gnu Emacs mode by Torbj\"orn Einarsson 
;;
;; This package is provided entirely 'as is'. I know it is not very good
;; Emacs lisp code - I don't even know lisp ! I just wrote it by copying
;; suitable features from other peoples packages. But it works, at least
;; provided gle -dx can be run as 'batch'. I am not interested
;; in debugging it for other people - if it does not work for you,
;; debug it yourself.
;;
;; To install this package, just put it somewhere in your emacs load
;; path. For instance, if you want to have ~/lisp in your load path,
;; put (setq load-path (append '("~/lisp") load-path )) in .emacs.
;; 
;; To auto-load gle.el further add 
;;
;; (autoload 'gle-mode "gle"
;;   "GLE editing mode" t)
;;   (setq auto-mode-alist (append auto-mode-alist 
;;			(list '("\\.gle$" . gle-mode))))
;; 
;; in your .emacs. After this, you should get the "GLE" mouse menu
;; in your emacs automatically every time you open a file with the
;; suffix .gle.
;; 
;; gle.el requires the lmenu.el emacs package
;;
(require 'lmenu)

(defun gle-ps-file ()
   "Command runs gle -dps on file" 
  (interactive)
  (save-buffer)
  (shell-command (concat "gle -dps " 
      (file-name-nondirectory (buffer-file-name)) ))
)  ;; End of defun gleps-file

(defun gle-eps-file ()
   "Command runs gle -deps on file" 
  (interactive)
  (save-buffer)
  (shell-command (concat "gle -deps " 
      (file-name-nondirectory (buffer-file-name)) ))
)  ;; End of defun gleps-file


(defun gle-ps-and-ghostview-file ()
   "Command runs gle -dps on file, then ghostviews it" 
  (interactive)
  (save-buffer)
  (shell-command (concat "gle /output=tmp.ps -dps  " 
      (file-name-nondirectory (buffer-file-name)) ))
  (shell-command "ghostview tmp.ps; rm tmp.ps" )
  (delete-other-windows)
)  ;; End of defun gle-ps-and-ghostview-file

(defun gle-ps-and-ghostview-file-lsc ()
   "Command runs gle -dps on file, then ghostviews it in landscape format" 
  (interactive)
  (save-buffer)
  (shell-command (concat "gle /output=tmp.ps -dps  " 
      (file-name-nondirectory (buffer-file-name)) ))
  (shell-command "ghostview -landscape tmp.ps; rm tmp.ps" )
  (delete-other-windows)
)  ;; End of defun gle-ps-and-ghostview-file


(defun gle-x-file ()
   "Command runs gle -dx on file" 
  (interactive)
  (save-buffer)
  (shell-command (concat "gle /DRA -dx " 
      (file-name-nondirectory (buffer-file-name))  ))
  ;; (delete-other-windows)
)  ;; End of defun gle-x-file


(defun gle-write-brief-commands ()
   "Command writes out gle commands for drawing a simple graph"
  (interactive)
  (insert "! Gle file generated by gle.el\n") 
  (insert "size 26 19\n") 
  (insert "set lwidth 0.06\nset hei 0.5\nset font psh\n")
  (insert "begin graph\n   nobox\n")
  (insert "   \n")
  (insert "   data file.gle d1\n")
  (insert "   \n")
  (insert "   d1 marker dot msize 0.7\n")
  (insert "   d1 line lstyle 1 key \"\"\n")
  (insert "   \n")
  (insert "   key hei 0.6 nobox\n")
  (insert "   \n")
  (insert "   !yaxis min 0 max 10 ! log\n")
  (insert "   !xaxis min 0 max 10 ! log\n")
  (insert "   \n")
  (insert "   xtitle \"\" dist 0.3 hei 0.6\n")
  (insert "   ytitle \"\" dist 0.3 hei 0.6\n")
  (insert "   ! x2title \"\"\n")
  (insert "   \n")
  (insert "end graph\n")

)  ;; End of defun gle-write-brief-commands

(defun gle-write-graph-commands ()
   "Command writes out the most used gle commands for drawing a graph"
  (interactive)
  (insert "! Gle file generated by gle.el\n") 
  (insert "size 26 19\n") 
  (insert "set lwidth 0.06\nset hei 0.5\nset font psh\n")
  (insert "begin graph\n   nobox\n")
  (insert "   \n")
  (insert "   data file.gle d1\n")
  (insert "   data file2.gle d2\n")
  (insert "   \n")
  (insert "   d1 marker dot msize 0.7\n")
  (insert "   d1 line lstyle 1 key \"\"\n")
  (insert "   d2 line lstyle 2 key \"\"\n")
  (insert "   \n")
  (insert "   key hei 0.6 nobox\n")
  (insert "   \n")
  (insert "   !yaxis min 0 max 10 log nolast\n")
  (insert "   !xaxis min 0 max 10 log nolast\n")
  (insert "   \n")
  (insert "   xtitle \"\" dist 0.3 hei 0.6\n")
  (insert "   ytitle \"\" dist 0.3 hei 0.6\n")
  (insert "   ! x2title \"\"\n")
  (insert "   \n")
  (insert "end graph\n")
  (insert "\n")
  (insert "set hei 0.6\n")
  (insert "amove 4.5 14.7\n")
  (insert "write \"\"\n")
  (insert "\n")
  (insert "set hei 0.3\n")
  (insert "amove 1 1\n")
  (insert "write DATE$\(\)\n")
  (insert "sub xarrow x1 y1 l\n")
  (insert "   amove x1 y1\n")
  (insert "   aline x1+l y1\n")
  (insert "   aline x1+l-l/4 y1+l/4\n")
  (insert "   amove x1+l y1\n")
  (insert "   aline x1+l-l/4 y1-l/4\n")
  (insert "   return 0\n")
  (insert "end sub\n")
  (insert "sub yarrow x1 y1 l\n")
  (insert "   amove x1 y1\n")
  (insert "   aline x1 y1+l\n")
  (insert "   aline x1+l/4 y1+l-l/4\n")
  (insert "   amove x1 y1+l\n")
  (insert "   aline x1-l/4 y1+l-l/4\n")
  (insert "   return 0\n")
  (insert "end sub\n")
  (insert "set join round\n")
  (insert "set lwidth 0.05\n")

)  ;; End of defun gle-write-graph-commands

(defun gle-write-publ-commands ()
   "Command writes out gle commands suitable for a publication graph"
  (interactive)
  (insert "! Gle file generated by gle.el\n") 
  (insert "size 26 19\n") 
  (insert "set lwidth 0.05\nset hei 0.5\nset font psh\n")
  (insert "begin graph\n   nobox\n")
  (insert "   \n")
  (insert "   data file.gle d1\n")
  (insert "   data file2.gle d2\n")
  (insert "   \n")
  (insert "   d1 marker dot msize 0.7\n")
  (insert "   d1 line lstyle 1 key \"\"\n")
  (insert "   d2 line lstyle 2 key \"\"\n")
  (insert "   \n")
  (insert "   key hei 1.0 nobox\n")
  (insert "   \n")
  (insert "   xlabels hei 0.8\n")
  (insert "   ylabels hei 0.8\n")
  (insert "   xticks length 0.3\n")
  (insert "   yticks length 0.3\n")
  (insert "   xsubticks off\n")
  (insert "   ysubticks off\n")
  (insert "   \n")
  (insert "   !yaxis min 0 max 10 log nolast\n")
  (insert "   !xaxis min 0 max 10 log nolast\n")
  (insert "   \n")
  (insert "   xtitle \"\" dist 0.3 hei 0.6\n")
  (insert "   ytitle \"\" dist 0.3 hei 0.6\n")
  (insert "   ! x2title \"\"\n")
  (insert "   \n")
  (insert "end graph\n")
  (insert "\n")
  (insert "set hei 1.0\n")
  (insert "amove 4.5 14.7\n")
  (insert "write \"\"\n")
  (insert "\n")
  (insert "set hei 0.3\n")
  (insert "amove 1 1\n")
  (insert "write DATE$\(\)\n")
  (insert "sub xarrow x1 y1 l\n")
  (insert "   amove x1 y1\n")
  (insert "   aline x1+l y1\n")
  (insert "   aline x1+l-l/4 y1+l/4\n")
  (insert "   amove x1+l y1\n")
  (insert "   aline x1+l-l/4 y1-l/4\n")
  (insert "   return 0\n")
  (insert "end sub\n")
  (insert "sub yarrow x1 y1 l\n")
  (insert "   amove x1 y1\n")
  (insert "   aline x1 y1+l\n")
  (insert "   aline x1+l/4 y1+l-l/4\n")
  (insert "   amove x1 y1+l\n")
  (insert "   aline x1-l/4 y1+l-l/4\n")
  (insert "   return 0\n")
  (insert "end sub\n")
  (insert "set join round\n")
  (insert "set lwidth 0.05\n")

)  ;; End of defun gle-write-publ-commands



(add-menu nil "GLE"
	  '(
            ["Xview file"                   gle-x-file t ]
	    ["Make postscript file"         gle-ps-file t]
	    ["Make EPS file"                gle-eps-file t]
	    ["Ghostview file"               gle-ps-and-ghostview-file t]
	    ["Ghostview file landsc."       gle-ps-and-ghostview-file-lsc t]
	    "--------------------"
            ["Write brief graph"            gle-write-brief-commands t]
            ["Write graph commands"         gle-write-graph-commands t]
            ["Write publication graph"      gle-write-publ-commands t]
            ))




(defun gle-mode ()
  "Major mode for editing gle Graphics Language Editor code.

Turning on gle mode calls the value of the variable `gle-mode-hook'
with no args, if that value is non-nil."
  (interactive)
  (kill-all-local-variables)
  (setq major-mode 'gle-mode)
  (setq mode-name "GLE")
  (local-set-key "\t" "   ")
  (global-set-key "\C-c\C-p" 'gle-ps-file)
  (global-set-key "\C-c\C-v" 'gle-ps-and-ghostview-file)
  (global-set-key "\C-c\C-x" 'gle-x-file)
  (global-set-key "\C-c\C-b" 'gle-write-basic-commands)
  (run-hooks 'gle-mode-hook)

)  ;; End of gle-mode

(provide 'gle)

;;; gle.el ends here